package br.com.pheat.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.pheat.beans.Usuario;
import br.com.pheat.dao.UsuarioDAO;
import javax.servlet.http.HttpSession;

public class AtualizaPerfilServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;       

	public AtualizaPerfilServelt() {
		super();        
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		tratarRequisicao(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		tratarRequisicao(request, response);
	}

	protected void tratarRequisicao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		try{			

			HttpSession session = request.getSession();

			Usuario usuario = (Usuario) session.getAttribute("UsuarioLogado");

			String nome = request.getParameter("nome");
			String email = request.getParameter("email");
			
			float altura = 0;
			if(!request.getParameter("altura").equals(""))
				altura = Float.parseFloat(request.getParameter("altura").replace(".", "").replace(",", "."));

			float peso = 0;
			if(!request.getParameter("peso").equals(""))
				peso = Float.parseFloat(request.getParameter("peso").replace(".", "").replace(",", "."));

			float calorias = 2000;
			if(!request.getParameter("calorias").equals(""))			
				calorias = Float.parseFloat(request.getParameter("calorias").replace(".", "").replace(",", "."));

			usuario.setNome(nome);
			usuario.setAltura(altura);
			usuario.setPeso(peso);
			usuario.setCalorias(calorias);
			usuario.setEmail(email);

			UsuarioDAO.atualizarUsuario(usuario);	
			response.sendRedirect("logado.jsp?m=a");

			session.setAttribute("UsuarioLogado", usuario);

		}catch(Exception e){
			response.sendRedirect("erro.jsp");
		}
	}
}
